#include "stdafx.h"
#include "stdlib/timespan.hpp"
#include <algorithm>

namespace JHCPP
{
	namespace stdlib
	{
		const CTimeSpan::TimeDiff CTimeSpan::MILLISECONDS = 1000;
		const CTimeSpan::TimeDiff CTimeSpan::SECONDS      = 1000*CTimeSpan::MILLISECONDS;
		const CTimeSpan::TimeDiff CTimeSpan::MINUTES      =   60*CTimeSpan::SECONDS;
		const CTimeSpan::TimeDiff CTimeSpan::HOURS        =   60*CTimeSpan::MINUTES;
		const CTimeSpan::TimeDiff CTimeSpan::DAYS         =   24*CTimeSpan::HOURS;

		CTimeSpan::CTimeSpan() : m_span(0)
		{
		}

		CTimeSpan::CTimeSpan(TimeDiff microseconds) : m_span(microseconds)
		{
		}

		CTimeSpan::CTimeSpan(long seconds, long microseconds) : m_span(TimeDiff(seconds)*SECONDS + microseconds)
		{
		}

		CTimeSpan::CTimeSpan(int days, int hours, int minutes, int seconds, int microseconds)
		:m_span(TimeDiff(microseconds) + TimeDiff(seconds)*SECONDS + TimeDiff(minutes)*MINUTES + TimeDiff(hours)*HOURS + TimeDiff(days)*DAYS)
		{
		}

		CTimeSpan::CTimeSpan(const CTimeSpan& timespan) : m_span(timespan.m_span)
		{
		}

		CTimeSpan::~CTimeSpan()
		{
		}

		CTimeSpan& CTimeSpan::operator = (const CTimeSpan& timespan)
		{
			m_span = timespan.m_span;
			return *this;
		}

		CTimeSpan& CTimeSpan::operator = (TimeDiff microseconds)
		{
			m_span = microseconds;
			return *this;
		}

		CTimeSpan& CTimeSpan::assign(int days, int hours, int minutes, int seconds, int microseconds)
		{
			m_span = TimeDiff(microseconds) + TimeDiff(seconds)*SECONDS + TimeDiff(minutes)*MINUTES + TimeDiff(hours)*HOURS + TimeDiff(days)*DAYS;
			return *this;
		}

		CTimeSpan& CTimeSpan::assign(long seconds, long microseconds)
		{
			m_span = TimeDiff(seconds)*SECONDS + TimeDiff(microseconds);
			return *this;
		}

		void CTimeSpan::swap(CTimeSpan& timespan)
		{
			std::swap(m_span, timespan.m_span);
		}

		bool CTimeSpan::operator == (const CTimeSpan& ts) const
		{
			return m_span == ts.m_span;
		}

		bool CTimeSpan::operator != (const CTimeSpan& ts) const
		{
			return m_span != ts.m_span;
		}

		bool CTimeSpan::operator >  (const CTimeSpan& ts) const
		{
			return m_span > ts.m_span;
		}

		bool CTimeSpan::operator >= (const CTimeSpan& ts) const
		{
			return m_span >= ts.m_span;
		}

		bool CTimeSpan::operator <  (const CTimeSpan& ts) const
		{
			return m_span < ts.m_span;
		}

		bool CTimeSpan::operator <= (const CTimeSpan& ts) const
		{
			return m_span <= ts.m_span;
		}

		bool CTimeSpan::operator == (TimeDiff microseconds) const
		{
			return m_span == microseconds;
		}

		bool CTimeSpan::operator != (TimeDiff microseconds) const
		{
			return m_span != microseconds;
		}

		bool CTimeSpan::operator >  (TimeDiff microseconds) const
		{
			return m_span > microseconds;
		}

		bool CTimeSpan::operator >= (TimeDiff microseconds) const
		{
			return m_span >= microseconds;
		}

		bool CTimeSpan::operator <  (TimeDiff microseconds) const
		{
			return m_span < microseconds;
		}

		bool CTimeSpan::operator <= (TimeDiff microseconds) const
		{
			return m_span <= microseconds;
		}

		CTimeSpan CTimeSpan::operator + (const CTimeSpan& d) const
		{
			return CTimeSpan(m_span + d.m_span);
		}

		CTimeSpan CTimeSpan::operator - (const CTimeSpan& d) const
		{
			return CTimeSpan(m_span - d.m_span);
		}

		CTimeSpan& CTimeSpan::operator += (const CTimeSpan& d)
		{
			m_span += d.m_span;
			return *this;
		}

		CTimeSpan& CTimeSpan::operator -= (const CTimeSpan& d)
		{
			m_span -= d.m_span;
			return *this;
		}

		CTimeSpan CTimeSpan::operator + (TimeDiff microseconds) const
		{
			return CTimeSpan(m_span + microseconds);
		}

		CTimeSpan CTimeSpan::operator - (TimeDiff microseconds) const
		{
			return CTimeSpan(m_span - microseconds);
		}

		CTimeSpan& CTimeSpan::operator += (TimeDiff microseconds)
		{
			m_span += microseconds;
			return *this;
		}

		CTimeSpan& CTimeSpan::operator -= (TimeDiff microseconds)
		{
			m_span -= microseconds;
			return *this;
		}

		int CTimeSpan::days() const
		{
			return int(m_span/DAYS);
		}

		int CTimeSpan::hours() const
		{
			return int((m_span/HOURS) % 24);
		}

		int CTimeSpan::totalHours() const
		{
			return int(m_span/HOURS);
		}

		int CTimeSpan::minutes() const
		{
			return int((m_span/MINUTES) % 60);
		}

		int CTimeSpan::totalMinutes() const
		{
			return int(m_span/MINUTES);
		}

		int CTimeSpan::seconds() const
		{
			return int((m_span/SECONDS) % 60);
		}

		int CTimeSpan::totalSeconds() const
		{
			return int(m_span/SECONDS);
		}

		int CTimeSpan::milliseconds() const
		{
			return int((m_span/MILLISECONDS) % 1000);
		}

		CTimeSpan::TimeDiff CTimeSpan::totalMilliseconds() const
		{
			return m_span/MILLISECONDS;
		}

		int CTimeSpan::microseconds() const
		{
			return int(m_span % 1000);
		}

		CTimeSpan::TimeDiff CTimeSpan::totalMicroseconds() const
		{
			return m_span;
		}

		int CTimeSpan::useconds() const
		{
			return int(m_span % 1000000);
		}

	}//end of namespace stdlib
}//end of namespace JHCPP
